package com.hmall.api.client.fallback;

import cn.hutool.log.Log;
import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.api.dto.OrderDetailDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Slf4j
//这个类是处理请求失败后的操作
public class ItemClientFallbackFactory implements FallbackFactory<ItemClient> {
    @Override
    public ItemClient create(Throwable cause) {
        return new ItemClient() {
            @Override
            public List<ItemDTO> queryItemsByIds(Collection<Long> ids) {
                log.error("商品查询失败!", cause);
                return Collections.emptyList();
            }

            @Override
            public void deductStock(List<OrderDetailDTO> detailDTOS) {
                log.error("扣减库存失败!", cause);
                throw new RuntimeException(cause);
            }

            @Override
            public void recoverStock(List<OrderDetailDTO> list) {
                log.error("恢复库存失败!", cause);
                throw new RuntimeException(cause);
            }

            @Override
            public ItemDTO queryItemById(Long id) {
                log.error("根据id查询商品失败,id为:{}", id, cause);
                return null;
            }
        };
    }
}
